package com.eduhzy.ttw.teacher.mvp.presenter;

import android.app.Application;
import android.os.Message;

import com.blankj.utilcode.util.SPUtils;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.CommonData;
import com.eduhzy.ttw.commonsdk.entity.UploadData;
import com.eduhzy.ttw.teacher.mvp.contract.PersonInfoContract;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.simple.eventbus.EventBus;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


@ActivityScope
public class PersonInfoPresenter extends BasePresenter<PersonInfoContract.Model, PersonInfoContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public PersonInfoPresenter(PersonInfoContract.Model model, PersonInfoContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void uploadAvatar(String file) {

        RxUtil.uploadImage(file, ArmsUtils.obtainAppComponentFromContext(mRootView.getActivity()).repositoryManager())
                .flatMap((Function<CommonData<UploadData>, Observable<CommonData<Map>>>) uploadDataCommonData -> {
                    Map<String, Object> map = new LinkedHashMap<>();
                    map.put("userId", SPUtils.getInstance().getString(Constants.USERID));
                    map.put("resId", uploadDataCommonData.getData().getId());
                    return mModel.updateUserTempAvatar(map);
                })
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(disposable -> {
                    mRootView.showUploadLoading(true);//显示下拉刷新的进度条
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.showUploadLoading(false);//隐藏下拉刷新的进度条
                })
                .subscribe(new ErrorHandleSubscriber<CommonData<Map>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<Map> upload) {
                        if (upload.isResult()) {
                            RxUtil.showTipDialog(mRootView.getActivity(), QMUITipDialog.Builder.ICON_TYPE_SUCCESS, upload.getMessage(), 500);
                            Message msg = Message.obtain();
                            msg.what = EventBusTags.UPDATE_AVATAR;
                            msg.obj = upload.getData().get(Constants.TEMPAVATAR);
                            SPUtils.getInstance().put(Constants.TEMPAVATAR, (String) upload.getData().get(Constants.TEMPAVATAR), SPUtils.getInstance().contains(Constants.TEMPAVATAR));
                            EventBus.getDefault().post(msg, RouterHub.APP_MINEFRAGMENT);
                            EventBus.getDefault().post(msg, RouterHub.APP_HOMEACTIVITY);
                        }

                    }
                });
    }


}
